<?php

namespace App\Http\Controllers;

use App\TableModel;
use Illuminate\Http\Request;

class TableController extends Controller
{
    public function korux()
    {
        $list = TableModel::get();
        return view('addtable', compact('list'));
    }


    public function add()
    {
        return view('add');
    }

    public function insert(Request $request)
    {
        $data = $request->input();
//        $data = $request->except("_token");
        unset($data['_token']);
        $data['hobby'] = implode("*", $data['hobby']);
        if ($data['id']==0){
            $ok = TableModel::create($data);
        }else{
            $ok = TableModel::where("id",$data['id'])->first();
            $ok->update($data);
        }

        if ($ok) {
            return "信息添加成功！<br> <a href='korux'> 返回首页 </a>";
        } else {
            return "信息添加失败！";
        }
    }


    public function del(Request $request)
    {
        $id=$request->input('id');
        $data=TableModel::where('id',$id)->first();
        $ok=$data->delete();
        if ($ok) {
            return "删除成功！<br><a href='korux'>返回首页</a> <br><a href='add'>添加信息</a>";
        }
    }


 public function update(Request $request)
 {
     $id = $request->input('id');
     $data = TableModel::where('id',$id)->first();
//     dd($data);
     return view("add",compact('data'));
 }
}































